package com.transpot.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.transpot.entry.order.*;
import com.transpot.service.order.OrderService;
import com.transpot.service.order.OrderStatusService;
import com.transpot.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订单模块的controller层
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderStatusService orderStatusService;

    /**
     * 添加订单信息方法
     * @param orderDTO
     * @return
     */
    @PostMapping("/addOrder")
    public R addOrder(@RequestBody OrderDTO orderDTO) {
        //调用service层自定义方法处理保存数据
        orderService.addOrder(orderDTO);
        return R.success();
    }

    /**
     * 获取全部未分配司机的订单
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAllOrderWithoutDriver")
    public R getAllOrderWithoutDriver(int current, int size) {
        //调用service层自定义方法查询订单数据
        Page<OrderBase> orderBaseList = orderService.getAllOrder(current, size);
        return R.success(orderBaseList);
    }

    /**
     * 获取全部已分配的订单
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getAllOrderIsAssigned")
    public R getAllOrderIsAssigned(int current, int size) {
        //调用service层自定义方法查询订单数据
        Page<OrderDriverVO> orderBaseList = orderService.getAllOrderIsAssigned(current, size);
        return R.success(orderBaseList);
    }

    /**
     * 获取全部配送中订单
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrderIsShipping")
    public R getOrderIsShipping(int current, int size) {
        //调用service层自定义方法查询订单数据
        Page<OrderDriverVO> orderBaseList = orderService.getOrderIsShipping(current, size);
        return R.success(orderBaseList);
    }
    /**
     * 获取全部已完成订单
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getOrderIsFinished")
    public R getOrderIsFinished(int current, int size) {
        //调用service层自定义方法查询订单数据
        Page<OrderDriverVO> orderBaseList = orderService.getOrderIsFinished(current, size);
        return R.success(orderBaseList);
    }

    /**
     * 接单功能
     * @return
     */
    @PostMapping("/orderReceive")
    public R orderReceive(Integer id, Integer driverId) {
        //设置订单整合类条件查询对象
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置条件为订单基本信息
        orderLambdaQueryWrapper.eq(Order::getBaseId, id);
        //查询订单
        Order order = orderService.getOne(orderLambdaQueryWrapper);
        //设置订单状态为配送中订单
        order.setStatus(2);
        //设置司机主键
        order.setDriverId(driverId);
        //修改id
        orderService.update(order, orderLambdaQueryWrapper);
        //创建订单状态对象
        OrderStatus orderStatus = new OrderStatus();
        //设置订单状态为待取货
        orderStatus.setStatus(0);
        //设置订单主键
        orderStatus.setBaseId(order.getBaseId());
        //调用mp原生方法插入订单状态类
        orderStatusService.save(orderStatus);
        return R.success();
    }

    /**
     * 根据登录账号的司机获取不同状态的订单
     * @param id
     * @return
     */
    @GetMapping("/getOrderById")
    public R getOrderById(int current, int size, Integer id, int status) {
        //调用service层自定义方法查询订单数据
        Page<OrderBase> orderBaseList = orderService.getOrderById(current, size, id, status);
        return R.success(orderBaseList);
    }

    /**
     * 拒绝订单方法
     * @param id
     * @return
     */
    @PostMapping("/refuseOrder")
    public R refuseOrder(Integer id) {
        //设置订单整合类的条件查询对象
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为与订单基本信息主键相同
        orderLambdaQueryWrapper.eq(Order::getBaseId, id);
        //执行查询
        Order order = orderService.getOne(orderLambdaQueryWrapper);
        //创建修改条件对象
        LambdaUpdateWrapper<Order> orderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //设置修改条件为司机主键设为null，车辆主键设为null，订单状态设为待分配
        orderLambdaUpdateWrapper.set(Order::getDriverId, null).
                set(Order::getCarId, null).
                set(Order::getStatus, 0).eq(Order::getBaseId, id);
        //修改订单整合类信息
        orderService.update(order, orderLambdaUpdateWrapper);
        return R.success();
    }

    /**
     * 获得各种订单数量
     * @return
     */
    @GetMapping("/getCount")
    public R getCount() {
        //调用service层自定义方法获取CountVO对象
        CountVO countVO = orderService.getCount();
        return R.success(countVO);
    }

    /**
     * 给订单分配司机的方法
     * @param divideDTO
     * @return
     */
    @PostMapping("/divideDriver")
    public R divideDriver(@RequestBody DivideDTO divideDTO) {
        //调用service层自定义方法完成分配订单
        orderService.divideDriver(divideDTO);
        return R.success();
    }

    /**
     * 根据订单id删除所有订单信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteOrderById")
    public R deleteOrderById(Integer id) {
        //调用service层的自定义方法删除订单
        orderService.deleteOrderById(id);
        return R.success();
    }

    /**
     * 根据订单id获取订单详细信息
     * @param id
     * @return
     */
    @GetMapping("/getOrderInfoById")
    public R getOrderInfoById(Integer id) {
        //调用service层方式实现查询订单详细信息
        OrderDTO orderDTO = orderService.getOrderInfoById(id);
        return R.success(orderDTO);
    }

    /**
     * 根据订单id修改订单信息
     * @param orderDTO
     * @return
     */
    @PostMapping("/updateOrderById")
    public R updateOrderById(@RequestBody OrderDTO orderDTO) {
        //调用service层方法完成订单修改
        orderService.updateOrderById(orderDTO);
        return R.success();
    }
}
